/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.hl_project.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.excel.ExcelImport;
import com.jeesite.common.validator.ValidatorUtils;
import com.jeesite.modules.sys.utils.DictUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.hl_project.entity.AjzProjectInfo;
import com.jeesite.modules.hl_project.dao.AjzProjectInfoDao;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintViolationException;

/**
 * 项目信息Service
 * @author cww
 * @version 2019-01-21
 */
@Service
@Transactional(readOnly=true)
public class AjzProjectInfoService extends CrudService<AjzProjectInfoDao, AjzProjectInfo> {
	private Map<String,String> tel ;//联系人电话
	private Map<String,String> addr;//地址
	private String id;
	/**
	 * 获取单条数据
	 * @param ajzProjectInfo
	 * @return
	 */
	@Override
	public AjzProjectInfo get(AjzProjectInfo ajzProjectInfo) {
		return super.get(ajzProjectInfo);
	}
	
	/**
	 * 查询分页数据
	 * @param ajzProjectInfo 查询条件
	 * @param ajzProjectInfo.page 分页对象
	 * @return
	 */
	@Override
	public Page<AjzProjectInfo> findPage(AjzProjectInfo ajzProjectInfo) {
		return super.findPage(ajzProjectInfo);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param ajzProjectInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(AjzProjectInfo ajzProjectInfo) {
		super.save(ajzProjectInfo);
	}
	
	/**
	 * 更新状态
	 * @param ajzProjectInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(AjzProjectInfo ajzProjectInfo) {
		super.updateStatus(ajzProjectInfo);
	}
	
	/**
	 * 删除数据
	 * @param ajzProjectInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(AjzProjectInfo ajzProjectInfo) {
		super.delete(ajzProjectInfo);
	}



	/**
	 * 导入用户数据
	 * @param file 导入的用户数据文件
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 */
	@Transactional(readOnly=false)
	public String importData(MultipartFile file, Boolean isUpdateSupport) {
		if (file == null){
			throw new ServiceException("请选择导入的数据文件！");
		}
		int successNum = 0; int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		try(ExcelImport ei = new ExcelImport(file, 2, 0)){
			List<AjzProjectInfo> list = ei.getDataList(AjzProjectInfo.class);
			//获取数据库的所有记录进行 项目名 判断
			List<AjzProjectInfo> ajzProjectInfoList = dao.findAllList();
			Map<String,String> name = new HashMap<String,String>();
			for (AjzProjectInfo ajzProjectInfo :ajzProjectInfoList){
				if(!StringUtils.isEmpty(ajzProjectInfo.getProjectName())){
					name.put(ajzProjectInfo.getProjectName(),ajzProjectInfo.getId());
				}
			}

			for (AjzProjectInfo ajzProjectInfo : list) {
				try{
					//判断该条记录是否修改 (cww注)
					boolean hasCompanyInfo = name.containsKey(ajzProjectInfo.getProjectName());

					if (isUpdateSupport){
						hasCompanyInfo = false;
					}
					//修改
					if (hasCompanyInfo){
						ajzProjectInfo.setId(name.get(ajzProjectInfo.getProjectName()));
						ajzProjectInfo.setStreet(DictUtils.getDictValue("hlq_street",ajzProjectInfo.getStreet(),"湖里街道"));
						ajzProjectInfo.setProjectType(DictUtils.getDictValue("project_type",ajzProjectInfo.getProjectType(),"建成投用"));
						this.update(ajzProjectInfo);
						successNum++;
						successMsg.append("<br/>" + successNum + "、账号 " + ajzProjectInfo.getProjectName() + " 更新成功");
					} else{
						ajzProjectInfo.setStreet(DictUtils.getDictValue("hlq_street",ajzProjectInfo.getStreet(),"湖里街道"));
						ajzProjectInfo.setProjectType(DictUtils.getDictValue("project_type",ajzProjectInfo.getProjectType(),"建成投用"));
						this.save(ajzProjectInfo);
						successNum++;
						successMsg.append("<br/>" + successNum + "、账号 " + ajzProjectInfo.getProjectName() + " 导入成功");
					}
//					if (isUpdateSupport)
//					else {
//						failureNum++;
//						failureMsg.append("<br/>" + failureNum + "、账号 " + ajzProjectInfo.getEnterpriseName() + " 已存在");
//					}
				} catch (Exception e) {
					failureNum++;
					String msg = "<br/>" + failureNum + "、账号 " + ajzProjectInfo.getProjectName() + " 导入失败：";
					if (e instanceof ConstraintViolationException){
						List<String> messageList = ValidatorUtils.extractPropertyAndMessageAsList((ConstraintViolationException)e, ": ");
						for (String message : messageList) {
							msg += message + "; ";
						}
					}else{
						msg += e.getMessage();
					}
					failureMsg.append(msg);
					logger.error(msg, e);
				}
			}
		} catch (Exception e) {
			failureMsg.append(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}else{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

}